package main

import (
	"fmt"
	"os"

	logger "gitee.com/woodpile/wgs.ths.log.go"
)

var (
	CodeLine     = false
	CodeLineLong = false
)

func main() {
	CodeLine = true
	// CodeLineLong = true

	SimpleLine()
	SimpleJson()
}

func SimpleLine() {
	cfg := &logger.LoggerConfig{
		TimeFormat:             "%Y-%m-%d %H:%M:%S.%f",
		TimeFormatBufferLength: 27,

		CodeLine:     CodeLine,
		CodeLineLong: CodeLineLong,
	}
	l, err := logger.NewLogger(cfg)
	if err != nil {
		fmt.Printf("Error exit: %v\n", err)
		os.Exit(1)
		return
	}
	// {
	// 	fn := "sample.log"
	// 	_, err := os.ReadFile(fn)
	// 	if err != os.ErrNotExist {
	// 		os.Remove(fn)
	// 	}
	// 	logFile, err := logger.NewTargetFile(&logger.TargetFileConfig{
	// 		Filename: "sample",
	// 	})
	// 	if err != nil {
	// 		fmt.Printf("Error exit: %v\n", err)
	// 		os.Exit(1)
	// 		return
	// 	}
	// 	l.AddTarget(logFile)
	// }

	Logging(l)
}

func SimpleJson() {
	cfg := &logger.LoggerConfig{
		TimeFormat:             "%Y-%m-%d %H:%M:%S.%f",
		TimeFormatBufferLength: 27,

		CodeLine:     CodeLine,
		CodeLineLong: CodeLineLong,

		IgnoreStdout: true,
		Formatter:    logger.DefaultFormatterJson,
		// Formatter: &logger.FormatterJson{
		// 	PrefixLikeMap: true,
		// },
	}
	l, err := logger.NewLogger(cfg)
	if err != nil {
		fmt.Printf("Error exit: %v\n", err)
		os.Exit(1)
		return
	}
	{
		fn := "sample.json.log"
		_, err := os.ReadFile(fn)
		if err != os.ErrNotExist {
			os.Remove(fn)
		}
		logFile, err := logger.NewTargetFile(&logger.TargetFileConfig{
			Filename: "sample.json",
		})
		if err != nil {
			fmt.Printf("Error exit: %v\n", err)
			os.Exit(1)
			return
		}
		l.AddTarget(logFile)
	}

	Logging(l)
}

func Logging(l *logger.Logger) {
	l.DBG().Z()
	l.DBG().Msg("Hello, world!").Z()

	l.DBG().Pre("long").Msg("Helloooooooooo, world!").Z()
	l.DBG().Msg("Helloooooooooo, world!").Pre("long").Z()

	l.DBG().Pre("pre").S("key", "value").Msg("Hello, world!").Z()
	l.DBG().Pre("pre").I("int", 87).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").U("uint", 87).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").D("float", 87.4).Msg("Hello, world!").Z()
	l.DBG().Pre("pre").I("int", 87).U("uint", 87).D("float", 87.4).S("key", "value").Msg("Hello, world!").Z()
	l.DBG().
		Pre("pre").
		I("int", 87).U("uint", 87).D("float", 87.4).
		S("key", "value").
		Msg("Hello, world!").Z()
	l.DBG().Msg("Get some error").Err(fmt.Errorf("error_content{\\\\\\}}]tt")).Z()

	l.AppendPrefix("logger_prefix")
	l.DBG().Msg("Hello, world!").Z()
	l.DBG().Pre("pre").Msg("Hello, world!").Z()

	l.AppendPrefixExclusive("lp_a", "uniq")
	l.DBG().Msg("Hello, world!").Z()
	l.AppendPrefixExclusive("lp_b", "uniq")
	l.DBG().Msg("Hello, world!").Z()

	l.Level = logger.LevelTrace
	l.TRC().Msg("Hello, world!").Z()
	l.DBG().Msg("Hello, world!").Z()
	l.INF().Msg("Hello, world!").Z()
	l.WRN().Msg("Hello, world!").Z()
	l.ERR().Msg("Hello, world!").Z()
	l.FAT().Msg("Hello, world!").Z()

	l.Level = logger.LevelInfo
	l.TRC().Msg("Hello, world!").Z()
	l.DBG().Msg("Hello, world!").Z()
	l.INF().Msg("Hello, world!").Z()
	l.WRN().Msg("Hello, world!").Z()
	l.ERR().Msg("Hello, world!").Z()
	l.FAT().Msg("Hello, world!").Z()
	l.Level = logger.DefaultGlobalLevel

	{ //Logger.Clone()
		_l := l.Clone()
		_l.WRN().Pre("clone").Msg("Hello, world!").Z()

		_l.Level = logger.LevelWarn
		_l.DBG().Pre("clone").Msg("Hello, world!").Z()
		l.DBG().Pre("pre").Msg("Hello, world!").Z()

	}
}
